import time

from selenium.webdriver.common.by import By

from base.FrontBase.front_base import BasePage, BaseHandle
from utils import UtilsDriver


class DetailsPage(BasePage):
    def __init__(self):
        super().__init__()
        self.one_cart = By.XPATH, "//*[text()='加入购物车']"
        self.two_cart = By.CSS_SELECTOR, "#join_cart"
        self.frame = By.CSS_SELECTOR, "#layui-layer-iframe1"
        self.msg = By.XPATH, "//*[@class='conect-title']/span"

    def find_one_cart_btn(self):
        return self.get_element(self.one_cart)

    def find_two_cart_btn(self):
        return self.get_element(self.two_cart)

    def find_frame(self):
        return self.get_element(self.frame)

    def find_msg(self):
        return self.get_element(self.msg)


class DetailsHandle(BaseHandle):
    def __init__(self):
        self.details_page = DetailsPage()

    def click_one_cart_btn(self):
        self.details_page.find_one_cart_btn().click()

    def click_two_cart_btn(self):
        self.details_page.find_two_cart_btn().click()

    def get_frame(self):
        UtilsDriver.get_front_driver().switch_to.frame(self.details_page.find_frame())

    def get_msg(self):
        return self.details_page.find_msg().text


class DetailsProxy:
    def __init__(self):
        self.detail_handle = DetailsHandle()

    def get_msg(self):
        self.detail_handle.click_one_cart_btn()
        self.detail_handle.click_two_cart_btn()
        time.sleep(2)
        self.detail_handle.get_frame()
        time.sleep(2)
        return self.detail_handle.get_msg()

